using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Configuration;
using System.Data;
using SimpleReminder.Properties;
using SimpleUtils.Win;

namespace SimpleReminder
{
    public partial class frmEvent : frmBase
    {
        DataRow _dr = null;

        public frmEvent(DataRow dr)
        {
            InitializeComponent();

            InitForm(dr);

            Program.MainForm.IsEditing = true;
        }

        public void InitForm(DataRow dr)
        {
            if (dr == null)
            {
                throw new ApplicationException("Invalid DataRow for Event");
            }

            Utils.InitTypeComboBox(cboType);
            Utils.InitReminderComboBox(cboReminder.Items);
            Utils.InitStatusComboBox(cboStatus.Items);
            cboStatus.SelectedIndex = 0;

            _dr = dr;

            DataView dv = MyEvents.GetEvents(_dr[MyEvents.COL_ID] as string);

            txtName.DataBindings.Add("Text", dv, MyEvents.COL_NAME, true, DataSourceUpdateMode.OnPropertyChanged);
            txtDescription.DataBindings.Add("Text", dv, MyEvents.COL_DESCRIPTION, true, DataSourceUpdateMode.OnPropertyChanged);
            cboType.DataBindings.Add("SelectedItem", dv, MyEvents.COL_TYPE, true, DataSourceUpdateMode.OnPropertyChanged);
            cboStatus.DataBindings.Add("SelectedItem", dv, MyEvents.COL_STATUS, true, DataSourceUpdateMode.OnPropertyChanged);

            dtpStartDate.DataBindings.Add("Value", dv, MyEvents.COL_STARTDATE, true, DataSourceUpdateMode.OnPropertyChanged);
            dtpEndDate.DataBindings.Add("Value", dv, MyEvents.COL_ENDDATE, true, DataSourceUpdateMode.OnPropertyChanged);

            cboReminder.DataBindings.Add("SelectedItem", dv, MyEvents.COL_REMINDER, true, DataSourceUpdateMode.OnPropertyChanged);

            nudFreq.DataBindings.Add("Value", dv, MyEvents.COL_FREQUENCE, true, DataSourceUpdateMode.OnPropertyChanged);
            nudWeeklyFreq.DataBindings.Add("Value", dv, MyEvents.COL_FREQUENCE, true, DataSourceUpdateMode.OnPropertyChanged);

            chkMon.DataBindings.Add("Checked", dv, MyEvents.COL_WEEKLYDAY_MON, true, DataSourceUpdateMode.OnPropertyChanged);
            chkTue.DataBindings.Add("Checked", dv, MyEvents.COL_WEEKLYDAY_TUE, true, DataSourceUpdateMode.OnPropertyChanged);
            chkWed.DataBindings.Add("Checked", dv, MyEvents.COL_WEEKLYDAY_WED, true, DataSourceUpdateMode.OnPropertyChanged);
            chkThu.DataBindings.Add("Checked", dv, MyEvents.COL_WEEKLYDAY_THU, true, DataSourceUpdateMode.OnPropertyChanged);
            chkFri.DataBindings.Add("Checked", dv, MyEvents.COL_WEEKLYDAY_FRI, true, DataSourceUpdateMode.OnPropertyChanged);
            chkSat.DataBindings.Add("Checked", dv, MyEvents.COL_WEEKLYDAY_SAT, true, DataSourceUpdateMode.OnPropertyChanged);
            chkSun.DataBindings.Add("Checked", dv, MyEvents.COL_WEEKLYDAY_SUN, true, DataSourceUpdateMode.OnPropertyChanged);

            rbType1.DataBindings.Add("Checked", dv, MyEvents.COL_MONTHLY_TYPE1, true, DataSourceUpdateMode.OnPropertyChanged);
            rbType2.DataBindings.Add("Checked", dv, MyEvents.COL_MONTHLY_TYPE2, true, DataSourceUpdateMode.OnPropertyChanged);

            nudMonthlyDay.DataBindings.Add("Value", dv, MyEvents.COL_MONTHLY_DAY, true, DataSourceUpdateMode.OnPropertyChanged);
            cboMonthlyWeekNo.DataBindings.Add("SelectedItem", dv, MyEvents.COL_MONTHLY_WEEKNO, true, DataSourceUpdateMode.OnPropertyChanged);
            cboMonthlyWeekDay.DataBindings.Add("SelectedItem", dv, MyEvents.COL_MONTHLY_WEEKDAY, true, DataSourceUpdateMode.OnPropertyChanged);

            chkJan.DataBindings.Add("Checked", dv, MyEvents.COL_MONTHLY_JAN, true, DataSourceUpdateMode.OnPropertyChanged);
            chkFeb.DataBindings.Add("Checked", dv, MyEvents.COL_MONTHLY_FEB, true, DataSourceUpdateMode.OnPropertyChanged);
            chkMar.DataBindings.Add("Checked", dv, MyEvents.COL_MONTHLY_MAR, true, DataSourceUpdateMode.OnPropertyChanged);
            chkApr.DataBindings.Add("Checked", dv, MyEvents.COL_MONTHLY_APR, true, DataSourceUpdateMode.OnPropertyChanged);
            chkMay.DataBindings.Add("Checked", dv, MyEvents.COL_MONTHLY_MAY, true, DataSourceUpdateMode.OnPropertyChanged);
            chkJun.DataBindings.Add("Checked", dv, MyEvents.COL_MONTHLY_JUN, true, DataSourceUpdateMode.OnPropertyChanged);
            chkJul.DataBindings.Add("Checked", dv, MyEvents.COL_MONTHLY_JUL, true, DataSourceUpdateMode.OnPropertyChanged);
            chkAug.DataBindings.Add("Checked", dv, MyEvents.COL_MONTHLY_AUG, true, DataSourceUpdateMode.OnPropertyChanged);
            chkSep.DataBindings.Add("Checked", dv, MyEvents.COL_MONTHLY_SEP, true, DataSourceUpdateMode.OnPropertyChanged);
            chkOct.DataBindings.Add("Checked", dv, MyEvents.COL_MONTHLY_OCT, true, DataSourceUpdateMode.OnPropertyChanged);
            chkNov.DataBindings.Add("Checked", dv, MyEvents.COL_MONTHLY_NOV, true, DataSourceUpdateMode.OnPropertyChanged);
            chkDec.DataBindings.Add("Checked", dv, MyEvents.COL_MONTHLY_DEC, true, DataSourceUpdateMode.OnPropertyChanged);
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            _dr.CancelEdit();

            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            err.Clear();
            bool hasError = false;

            if (String.IsNullOrEmpty(txtName.Text))
            {
                err.SetError(txtName, "Please input Name.");
                hasError = true;                
            }
            if (String.IsNullOrEmpty(txtDescription.Text))
            {
                err.SetError(txtDescription, "Please input Description.");
                hasError = true;
            }

            if (hasError) return;

            MyEvents.EventTypes et = Utils.ParseEventType(cboType.SelectedItem as string);
            switch(et) 
            {
                case MyEvents.EventTypes.Minutely:
                    _dr[MyEvents.COL_LASTRUNTIME] = DateTime.Now;
                    break;
                default:
                    _dr[MyEvents.COL_LASTRUNTIME] = System.Windows.Forms.DateTimePicker.MinimumDateTime;
                    break;
            }
            
            _dr.EndEdit();
            MyEvents.Save();

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void cboType_SelectedIndexChanged(object sender, EventArgs e)
        {
            MyEvents.EventTypes et = Utils.ParseEventType(cboType.SelectedItem as string);
            switch (et)
            {                    
                case MyEvents.EventTypes.Secondly:
                case MyEvents.EventTypes.Minutely:
                case MyEvents.EventTypes.Daily:
                case MyEvents.EventTypes.Hourly:
                case MyEvents.EventTypes.Yearly:
                    gbFreq.Text = "Schedule Task " + et.ToString();
                    gbFreq.Visible = true;
                    gbWeekly.Visible = false;
                    gbMonthly.Visible = false;
                    switch (et)
                    {
                        case MyEvents.EventTypes.Secondly:
                            lblFreq.Text = "second(s)";
                            break;
                        case MyEvents.EventTypes.Minutely:
                            lblFreq.Text = "minute(s)";
                            break;
                        case MyEvents.EventTypes.Daily:
                            lblFreq.Text = "day(s)";
                            break;
                        case MyEvents.EventTypes.Hourly:
                            lblFreq.Text = "hour(s)";
                            break;
                        case MyEvents.EventTypes.Yearly:
                            lblFreq.Text = "year(s)";
                            break;
                        default:
                            lblFreq.Text = "";
                            break;
                    }
                    break;
                case MyEvents.EventTypes.Monthly:
                    gbFreq.Visible = false;
                    gbWeekly.Visible = false;
                    gbMonthly.Visible = true;
                    break;
                case MyEvents.EventTypes.Weekly:
                    gbFreq.Visible = false;
                    gbWeekly.Visible = true;
                    gbMonthly.Visible = false;
                    break;
                default:
                    gbFreq.Visible = false;
                    gbWeekly.Visible = false;
                    gbMonthly.Visible = false;
                    break;
            }

            if (et == MyEvents.EventTypes.Once)
            {
                dtpEndDate.Enabled = false;
            }
            else
            {
                dtpEndDate.Enabled = true;
            }
        }

        private void SetMonthlyType()
        {
            if (rbType1.Checked)
            {
                nudMonthlyDay.Enabled = true;
                cboMonthlyWeekNo.Enabled = false;
                cboMonthlyWeekDay.Enabled = false;
            }
            else
            {
                nudMonthlyDay.Enabled = false;
                cboMonthlyWeekNo.Enabled = true;
                cboMonthlyWeekDay.Enabled = true;
            }
            if (cboMonthlyWeekNo.SelectedIndex < 0) cboMonthlyWeekNo.SelectedIndex = 0;
            if (cboMonthlyWeekDay.SelectedIndex < 0) cboMonthlyWeekDay.SelectedIndex = 0;
        }

        private void rbType1_CheckedChanged(object sender, EventArgs e)
        {
            SetMonthlyType();
        }

        private void lblReminder_Click(object sender, EventArgs e)
        {
            switch (cboReminder.SelectedItem as string)
            {
                case MyEvents.REMINDER_AGENT:
                    {
                        frmReminderAgent f = new frmReminderAgent();
                        f.AgentSpeech = txtDescription.Text;
                        f.AgentTop = Convert.ToInt16(_dr[MyEvents.COL_AGENT_TOP]);
                        f.AgentLeft = Convert.ToInt16(_dr[MyEvents.COL_AGENT_LEFT]);
                        f.AgentPath = _dr[MyEvents.COL_AGENT_PATH] as string;
                        f.AgentAnimation = _dr[MyEvents.COL_AGENT_ANIMATION] as string;

                        if (f.ShowDialog() == DialogResult.OK)
                        {
                            _dr[MyEvents.COL_REMINDER] = MyEvents.REMINDER_AGENT; // necessary
                            _dr[MyEvents.COL_AGENT_PATH] = f.AgentPath;
                            _dr[MyEvents.COL_AGENT_ANIMATION] = f.AgentAnimation;
                            _dr[MyEvents.COL_AGENT_TOP] = f.AgentTop;
                            _dr[MyEvents.COL_AGENT_LEFT] = f.AgentLeft;
                        }
                    }
                    break;
                case MyEvents.REMINDER_EMAIL:
                    {
                        frmReminderEmail f = new frmReminderEmail();
                        f.EmailTo = _dr[MyEvents.COL_EMAIL_TO] as string;
                        f.EmailCc = _dr[MyEvents.COL_EMAIL_CC] as string;
                        f.EmailBcc = _dr[MyEvents.COL_EMAIL_BCC] as string;
                        f.EmailSubject = _dr[MyEvents.COL_NAME] as string;
                        f.EmailBody = _dr[MyEvents.COL_DESCRIPTION] as string;

                        if (f.ShowDialog() == DialogResult.OK)
                        {
                            _dr[MyEvents.COL_REMINDER] = MyEvents.REMINDER_EMAIL; 
                            _dr[MyEvents.COL_EMAIL_TO] = f.EmailTo;
                            _dr[MyEvents.COL_EMAIL_CC] = f.EmailCc;
                            _dr[MyEvents.COL_EMAIL_BCC] = f.EmailBcc;
                        }
                    }
                    break;
                case MyEvents.REMINDER_APP:
                    {
                        frmReminderApp f = new frmReminderApp();
                        f.AppPath = _dr[MyEvents.COL_APP_PATH] as string;
                        f.AppArgument = _dr[MyEvents.COL_APP_ARG] as string;

                        if (f.ShowDialog() == DialogResult.OK)
                        {
                            _dr[MyEvents.COL_REMINDER] = MyEvents.REMINDER_APP;
                            _dr[MyEvents.COL_APP_PATH] = f.AppPath;
                            _dr[MyEvents.COL_APP_ARG] = f.AppArgument;
                        }
                    }
                    break;
                default:
                    SimpleMessage.ShowInfo("Not implemented.");                   
                    break;
            }
        }

        private void frmEvent_FormClosed(object sender, FormClosedEventArgs e)
        {
            Program.MainForm.IsEditing = false;
        }

    } // end of class
}